// format.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
int BCDToInt(unsigned char *ptr, int len)
{  int i;
    int value = 0;
    for(i=len-1;i>=0;i--)
    {
        value += ((ptr[i]&0xf0)*pow(10,2*i+1)+(ptr[i]&0x0f)*pow(10,2*i));
    }
    return value; 
}
unsigned long  BCDToDec(const unsigned char *bcd, int length)   
{   
    int tmp;   
    unsigned long dec = 0;   
      
    for(int i = 0; i < length; i++)   
    {   
        tmp = ((bcd[i] >> 4) & 0x0F) * 10 + (bcd[i] & 0x0F);      
        dec += tmp * pow(100, length - 1 - i);             
    }   
      
    return dec;   
} 
unsigned long  BCDToInt2(const unsigned char *bcd, int length)   
{   
    int tmp;   
    unsigned long dec = 0;   
      
    for(int i = length-1; i >= 0; i--)   
    {   
        tmp = ((bcd[i] >> 4) & 0x0F) * 10 + (bcd[i] & 0x0F);      
        dec += tmp * pow(100, length - 1 - i);             
    }   
      
    return dec;   
}  
 

int main(int argc, char* argv[])
{
	unsigned char c[1];
	unsigned char a[2];
	c[0] = 0x80;
	a[0] = 0x01;
	a[1] = 0x30;
	unsigned char d[2];
	d[0] = 0x30;
	d[1] = 0x01;
	unsigned char b = 0x80;
	int value;
	value = BCDToDec(&c[0],1);
	printf("value = %d\n",value);
	value = BCDToDec(&b,1);
	printf("value = %d\n",value);
	value = BCDToDec(&a[0],2);
    printf("value = %d\n",value);
	value = BCDToInt2(&d[0],2);
    printf("value = %d\n",value);
	
	return 0;
}
